package com.ma.cloud.api;

import com.ma.cloud.common.utils.RedisLettuceUtil;
import com.netease.nim.camellia.hot.key.sdk.CamelliaHotKeyCacheSdk;
import com.netease.nim.camellia.hot.key.sdk.CamelliaHotKeyMonitorSdk;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * redis压力测试接口
 * @author mr
 * @since 2025-06-23
 */
@Slf4j
@RestController
@RequestMapping("/redis")
@AllArgsConstructor
public class RedisHotKeyApi {

    /**
     * CamelliaHotKeyMonitorSdk（仅用于监控热key）
     */
    private final CamelliaHotKeyMonitorSdk monitorSdk;

    /**
     * CamelliaHotKeyCacheSdk（用于监控热key，并且提供本地缓存功能）
     */
    private final CamelliaHotKeyCacheSdk cacheSdk;

    /**
     * redisLettuceUtil
     */
    private final RedisLettuceUtil redisLettuceUtil;


    @GetMapping("/pressure/hotkey")
    public Object getValue(@RequestParam String namespace, @RequestParam String key) {

        //使用热key工具,监控热key，在检测热key后，会自动走本地缓存逻辑，冷key/监控服务不可用，会走远程redis逻辑
        Object value = cacheSdk.getValue(namespace, key, k -> redisLettuceUtil.get(key));

        log.info("namespace: {} get {} value is : {}", namespace, key, value);
        return value;
    }

    @GetMapping("/pressure/direct")
    public Object set(@RequestParam String key) {

        //直连redis客户端
        Object value = redisLettuceUtil.get(key);

        log.info("redis get {} value is : {}", key, value);
        return value;
    }

    @GetMapping("/set")
    public boolean set(@RequestParam String key, @RequestParam String value) {

        boolean result = redisLettuceUtil.set(key, value);
        log.info("redis set key {} value is : {}", key, value);
        return result;
    }

}
